home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 3 / Amiga Format CD03 (1996-07-04)(Future Publishing)(GB)(Track 1 of 6)[!][issue 1996-08].iso / pd / utilities / grabkey / grabkey.c < prev    next >
C/C++ Source or Header  |  1996-05-20  |  20KB  |  716 lines

  1. /*
  2. **  $VER: GrabKey 1.0 (08 May 1996)  **
  3. **
  4. **        © 1996 Timo C. Nentwig
  5. **          all rights reserved !
  6. **
  7. ** ======================================
  8. **
  9. **  Language:
  10. **  ¯¯¯¯¯¯¯¯
  11. **
  12. **    Program is compiled by SAS/C
  13. **
  14. **
  15. **  Purpose:
  16. **  ¯¯¯¯¯¯¯
  17. **
  18. **    Grab/Print active  window or screen
  19. **    by hotkey.
  20. **
  21. **  Requirements:
  22. **  ¯¯¯¯¯¯¯¯¯¯¯¯
  23. **
  24. **    · AmigaOS v39+
  25. **    · datatypes.library
  26. **
  27. **  Bugs:
  28. **  ¯¯¯¯
  29. **
  30. **
  31. **
  32. **  ToDo:
  33. **  ¯¯¯¯
  34. **
  35. **
  36. **
  37. **  Notes:
  38. **  ¯¯¯¯¯
  39. **
  40. **
  41. ** ======================================
  42. **
  43. **  History:
  44. **  ¯¯¯¯¯¯¯
  45. **
  46. **  08 May 1996 - 1.0 : initial release
  47. **  08 May 1996 - 1.1 : can also grab active screen
  48. **                      OUTPATH -> FILE
  49. **
  50. */
  51.  
  52. /// #include
  53.  
  54. #include    <datatypes/pictureclass.h>
  55. #include    <dos/dos.h>
  56. #include    <exec/types.h>
  57. #include    <exec/memory.h>
  58. #include    <graphics/gfxbase.h>
  59. #include    <intuition/intuition.h>
  60. #include    <intuition/intuitionbase.h>
  61.  
  62. #include    <libraries/commodities.h>
  63.  
  64. #include    <proto/commodities.h>
  65. #include    <proto/datatypes.h>
  66. #include    <proto/dos.h>
  67. #include    <proto/exec.h>
  68. #include    <proto/graphics.h>
  69. #include    <proto/intuition.h>
  70.  
  71. #include    <stdio.h>
  72. #include    <strings.h>
  73.  
  74. ///
  75. /// #define
  76.  
  77. #define    EVT_WINKEY        1L
  78. #define    EVT_SCRKEY        2L
  79. #define    EVT_PRTKEY        3L
  80. #define    EVT_PRTKEY_SCR    4L
  81.  
  82. #define    PRG_VERSION       "1.1"
  83. #define    PRG_TITLE         "GrabKey"
  84. #define    PRG_AUTHOR        "Timo C. Nentwig"
  85. #define    PRG_YEAR          "1996"
  86.  
  87. #define    MAX(a,b)          ((a) > (b) ? (a) : (b))
  88.  
  89. ///
  90. /// Prototypes
  91.  
  92. VOID        ProcessMsg      (VOID);
  93. BOOL        AttachFilter    (STRPTR onkey, ULONG user_event);
  94. VOID        SavePicture     (Object *Picture, STRPTR FileName);
  95. VOID        PrintPicture    (Object *Picture);
  96. Object *    GetPicture      (BOOL fullscreen);
  97.  
  98. ///
  99.  
  100. struct    IntuitionBase   *IntuitionBase;
  101. struct    GfxBase         *GfxBase;
  102. struct    Library         *DataTypesBase;
  103. struct    Library         *CxBase;
  104. struct    MsgPort         *broker_mp;
  105. CxObj                     *broker;
  106. ULONG                      cxsigflag;
  107. STRPTR                     filename;
  108.  
  109. struct    Picture
  110. {
  111.  
  112.     UWORD    Left;
  113.     UWORD    Top;
  114.     UWORD    Width;
  115.     UWORD    Height;
  116.     UWORD    PageWidth;
  117.     UWORD    PageHeight;
  118.     UWORD    Depth;
  119.  
  120. };
  121.  
  122. /// main ()
  123.  
  124. VOID
  125. main (UWORD argc, STRPTR *argv)
  126. {
  127.  
  128.     if (IntuitionBase = (struct IntuitionBase *) OpenLibrary ("intuition.library", 39))
  129.     {
  130.  
  131.         if (DOSBase = (struct DosLibrary *) OpenLibrary ("dos.library", 39))
  132.         {
  133.  
  134.             if (GfxBase = (struct GfxBase *) OpenLibrary ("graphics.library", 39))
  135.             {
  136.  
  137.                 if (DataTypesBase = OpenLibrary ("datatypes.library", 39))
  138.                 {
  139.  
  140.                     if (CxBase = OpenLibrary ("commodities.library", 39))
  141.                     {
  142.  
  143.                         if (broker_mp = CreateMsgPort())
  144.                         {
  145.  
  146.                             STRPTR   *ttypes;
  147.                             STRPTR    winkey;
  148.                             STRPTR    scrkey;
  149.                             STRPTR    prtkey;
  150.                             STRPTR    prtkey_scr;
  151.                             struct    NewBroker    newbroker =
  152.                             {
  153.  
  154.                                 NB_VERSION,
  155.                                 PRG_TITLE,
  156.                                 PRG_TITLE" "PRG_VERSION" © "PRG_YEAR" by "PRG_AUTHOR,
  157.                                 "Save/Print active window by hotkey",
  158.                                 NBU_UNIQUE | NBU_NOTIFY,
  159.                                 0, 0, 0, 0
  160.  
  161.                             };
  162.  
  163.                             newbroker.nb_Port = broker_mp;
  164.                             cxsigflag         = 1L << broker_mp->mp_SigBit;
  165.  
  166.                             ttypes            = ArgArrayInit (argc, argv);
  167.                             newbroker.nb_Pri  = (BYTE) ArgInt (ttypes, "CX_PRIORITY", 0);
  168.                             winkey            =     ArgString (ttypes, "WINKEY",     "LALT F1");
  169.                             scrkey            =     ArgString (ttypes, "SCRKEY",     "LALT F2");
  170.                             prtkey            =     ArgString (ttypes, "PRTKEY",     "LALT F3");
  171.                             prtkey_scr        =     ArgString (ttypes, "PRTKEY_SCR", "LALT F4");
  172.                             filename          =     ArgString (ttypes, "FILE",       "ram:"PRG_TITLE".IFF");
  173.  
  174.                             if (broker = CxBroker (&newbroker, NULL))
  175.                             {
  176.  
  177.                                 CxMsg   *msg;
  178.  
  179.                                 if (AttachFilter (winkey, EVT_WINKEY) &&
  180.                                     AttachFilter (scrkey, EVT_SCRKEY) &&
  181.                                     AttachFilter (prtkey, EVT_PRTKEY) &&
  182.                                     AttachFilter (prtkey, EVT_PRTKEY_SCR))
  183.                                 {
  184.  
  185.                                     ActivateCxObj (broker, 1L);
  186.                                     ProcessMsg();
  187.  
  188.                                 }
  189.  
  190.                                 DeleteCxObjAll (broker);
  191.  
  192.                                 while (msg = (CxMsg *) GetMsg (broker_mp))         // Empty the port of all CxMsgs
  193.                                     ReplyMsg ((struct Message *) msg);
  194.  
  195.                             }
  196.  
  197.                             DeletePort(broker_mp);
  198.  
  199.                         }
  200.  
  201.                         ArgArrayDone();
  202.                         CloseLibrary (CxBase);
  203.  
  204.                     }
  205.                     else
  206.                     {
  207.  
  208.                         printf ("ERROR: Couldn't open commodities.library v39+\n");
  209.  
  210.                     }
  211.  
  212.                     CloseLibrary ((struct Library *) DataTypesBase);
  213.  
  214.  
  215.                 }
  216.                 else
  217.                 {
  218.  
  219.                     printf ("ERROR: Couldn't open datatypes.library v39+\n");
  220.  
  221.                 }
  222.  
  223.  
  224.                 CloseLibrary ((struct Library *) GfxBase);
  225.  
  226.             }
  227.             else
  228.             {
  229.  
  230.                 printf ("ERROR: Couldn't open graphics.library v39+\n");
  231.  
  232.             }
  233.  
  234.             CloseLibrary ((struct Library *) DOSBase);
  235.  
  236.         }
  237.  
  238.         CloseLibrary ((struct Library *) IntuitionBase);
  239.  
  240.     }
  241.     else
  242.     {
  243.  
  244.         printf ("ERROR: Couldn't open intuition.library v39+\n");
  245.  
  246.     }
  247.  
  248. }
  249.  
  250. ///
  251. /// ProcessMsg ()
  252.  
  253.     /*
  254.      *    FROM        /rkm/libs/commodities/hotkey.c
  255.      *
  256.      *    FUNCTION    Process commodity messages.
  257.      *
  258.      *    NOTE
  259.      *
  260.      *    EXAMPLE     ProcessMsg ();
  261.      *
  262.      */
  263.  
  264.  
  265. VOID
  266. ProcessMsg (VOID)
  267. {
  268.  
  269.     CxMsg    *msg;
  270.     Object   *Picture;
  271.     ULONG     sigrcvd;
  272.     ULONG     msgid;
  273.     ULONG     msgtype;
  274.     LONG      returnvalue = 1L;
  275.  
  276.     while (returnvalue)
  277.     {
  278.  
  279.         sigrcvd = Wait (SIGBREAKF_CTRL_C | cxsigflag);
  280.  
  281.         while (msg = (CxMsg *) GetMsg (broker_mp))
  282.         {
  283.  
  284.             msgid   = CxMsgID   (msg);
  285.             msgtype = CxMsgType (msg);
  286.             ReplyMsg ((struct Message *) msg);
  287.  
  288.             switch (msgtype)
  289.             {
  290.  
  291.                 case CXM_IEVENT:
  292.  
  293.                     switch (msgid)
  294.                     {
  295.  
  296.                         case EVT_WINKEY:                                   // window
  297.  
  298.                             if (Picture = GetPicture (FALSE))
  299.                             {
  300.  
  301.                                 SavePicture (Picture, filename);
  302.  
  303.                             }
  304.                             break;
  305.  
  306.                         case EVT_SCRKEY:                                   // screen
  307.  
  308.                             if (Picture = GetPicture (TRUE))
  309.                             {
  310.  
  311.                                 SavePicture (Picture, filename);
  312.  
  313.                             }
  314.                             break;
  315.  
  316.  
  317.  
  318.  
  319.                         case EVT_PRTKEY:                                   // window
  320.  
  321.                             if (Picture = GetPicture (FALSE))
  322.                             {
  323.  
  324.                                 PrintPicture (Picture);
  325.  
  326.                             }
  327.                             break;
  328.  
  329.                         case EVT_PRTKEY_SCR:                               // screen
  330.  
  331.                             if (Picture = GetPicture (TRUE))
  332.                             {
  333.  
  334.                                 PrintPicture (Picture);
  335.  
  336.                             }
  337.                             break;
  338.  
  339.                     }
  340.                     break;
  341.  
  342.                 case CXM_COMMAND:
  343.  
  344.                     switch (msgid)
  345.                     {
  346.  
  347.                         case CXCMD_DISABLE:
  348.  
  349.                             ActivateCxObj (broker, 0L);
  350.                             break;
  351.  
  352.                         case CXCMD_ENABLE:
  353.  
  354.                             ActivateCxObj (broker, 1L);
  355.                             break;
  356.  
  357.                         case CXCMD_KILL:
  358.  
  359.                             returnvalue = 0L;
  360.                             break;
  361.  
  362.                         case CXCMD_UNIQUE:
  363.  
  364.                             returnvalue = 0L;
  365.                             break;
  366.  
  367.                     }
  368.                     break;
  369.  
  370.             }
  371.  
  372.         }
  373.  
  374.         if (sigrcvd & SIGBREAKF_CTRL_C)
  375.             returnvalue = 0L;
  376.  
  377.     }
  378.  
  379. }
  380.  
  381. ///
  382. /// AttachFilter ()
  383.  
  384.     /*
  385.      *    FROM        /CenterWin/CenterWin.c
  386.      *
  387.      *    FUNCTION    Attach a filter.
  388.      *
  389.      *    NOTE
  390.      *
  391.      *    EXAMPLE     AttachFilter (hotkey, EVT_HOTKEY);
  392.      *
  393.      */
  394.  
  395.  
  396. BOOL
  397. AttachFilter (STRPTR onkey, ULONG user_event)
  398. {
  399.  
  400.     CxObj   *filter;
  401.     CxObj   *sender;
  402.     CxObj   *translator;
  403.  
  404.  
  405.     if (filter = CxFilter (onkey))
  406.     {
  407.  
  408.         AttachCxObj (broker, filter);
  409.  
  410.        if (sender = CxSender (broker_mp, user_event))
  411.        {
  412.  
  413.            AttachCxObj (filter, sender);
  414.  
  415.            if (translator = CxTranslate (NULL))
  416.            {
  417.  
  418.                AttachCxObj (filter, translator);
  419.  
  420.                if ( ! (CxObjError (filter)))
  421.                    return (TRUE);
  422.  
  423.            }
  424.  
  425.        }
  426.  
  427.     }
  428.  
  429.     return (FALSE);
  430.  
  431. }
  432.  
  433. ///
  434. /// SavePicture ()
  435.  
  436.     /*
  437.      *    FUNCTION    Save an Object *Picture.
  438.      *
  439.      *    NOTE
  440.      *
  441.      *    EXAMPLE     SavePicture (Picture, "ram:file");
  442.      *
  443.      */
  444.  
  445.  
  446. VOID
  447. SavePicture (Object *Picture, STRPTR FileName)
  448. {
  449.  
  450.     BPTR    FileHandle;
  451.  
  452.     if (FileHandle = Open (FileName, MODE_NEWFILE))
  453.     {
  454.  
  455.         if (DoMethod (Picture, DTM_WRITE, NULL, FileHandle, DTWM_IFF, NULL))
  456.         {
  457.  
  458.             Close (FileHandle);
  459.  
  460.         }
  461.         else
  462.         {
  463.  
  464.             Close      (FileHandle);
  465.             DeleteFile (FileName);
  466.  
  467.         }
  468.  
  469.     }
  470.  
  471.     DisposeDTObject (Picture);
  472.  
  473. }
  474.  
  475. ///
  476. /// PrintPicture ()
  477.  
  478.     /*
  479.      *    FUNCTION    Print an Object *Picture.
  480.      *
  481.      *    NOTE
  482.      *
  483.      *    EXAMPLE     PrintPicture (Picture);
  484.      *
  485.      */
  486.  
  487.  
  488. VOID
  489. PrintPicture (Object *Picture)
  490. {
  491.  
  492.     struct    MsgPort   *PrinterPort;
  493.  
  494.  
  495.     if (PrinterPort = CreateMsgPort())
  496.     {
  497.  
  498.         union    printerIO   *PrinterIO;
  499.  
  500.         if (PrinterIO =  (union printerIO *) CreateIORequest (PrinterPort, sizeof (union printerIO)))
  501.         {
  502.  
  503.             if ( ! (OpenDevice ("printer.device", 0, (struct IORequest *) PrinterIO, NULL)))
  504.             {
  505.  
  506.                 DoMethod (Picture, DTM_PRINT, NULL, PrinterIO, NULL);
  507.                 CloseDevice ((struct IORequest *) PrinterIO);
  508.  
  509.             }
  510.  
  511.             DeleteIORequest ((struct IORequest *) PrinterIO);
  512.  
  513.         }
  514.  
  515.         DeleteMsgPort (PrinterPort);
  516.     }
  517.  
  518.     DisposeDTObject (Picture);
  519.  
  520. }
  521.  
  522. ///
  523. /// GetPicture ()
  524.  
  525.     /*
  526.      *    FUNCTION    Get bitmap for picture.
  527.      *
  528.      *    NOTE
  529.      *
  530.      *    EXAMPLE     Object *Picture = GetPicture();
  531.      *
  532.      */
  533.  
  534.  
  535. Object *
  536. GetPicture  (BOOL fullscreen)
  537. {
  538.  
  539.     struct    RastPort   *RPort;
  540.  
  541.  
  542.     if (RPort = (struct RastPort *) AllocVec (sizeof (struct RastPort), MEMF_ANY))
  543.     {
  544.  
  545.         struct    Picture   *Pic;
  546.  
  547.         if (Pic = AllocVec (sizeof (struct Picture), MEMF_ANY))
  548.         {
  549.  
  550.             struct    BitMap   *BitMap;
  551.             struct    Window   *Win;
  552.             struct    Screen   *Scr;
  553.             ULONG               IntuiLock;
  554.             BOOL                Locked = TRUE;
  555.  
  556.             InitRastPort (RPort);
  557.  
  558.             IntuiLock = LockIBase (NULL);
  559.  
  560.             Win = IntuitionBase -> ActiveWindow;
  561.             Scr = IntuitionBase -> ActiveScreen;
  562.  
  563.             Pic -> PageWidth  = Scr -> Width;
  564.             Pic -> PageHeight = Scr -> Height;
  565.  
  566.             if (fullscreen)
  567.             {
  568.  
  569.                 Pic -> Left       = Scr -> LeftEdge;
  570.                 Pic -> Top        = Scr -> TopEdge;
  571.                 Pic -> Width      = Scr -> Width;
  572.                 Pic -> Height     = Scr -> Height;
  573.                 Pic -> Depth      = GetBitMapAttr (Scr -> RastPort . BitMap, BMA_DEPTH);
  574.  
  575.                 BitMap = AllocBitMap (Pic -> Width, Pic -> Height, Pic -> Depth, BMF_CLEAR, Scr -> RastPort . BitMap);
  576.  
  577.             }
  578.             else
  579.             {
  580.  
  581.                 Pic -> Left       = Win -> LeftEdge;
  582.                 Pic -> Top        = Win -> TopEdge;
  583.                 Pic -> Width      = Win -> Width;
  584.                 Pic -> Height     = Win -> Height;
  585.                 Pic -> Depth      = GetBitMapAttr (Win -> RPort -> BitMap, BMA_DEPTH);
  586.  
  587.                 BitMap = AllocBitMap (Pic -> Width, Pic -> Height, Pic -> Depth, BMF_CLEAR, Win -> RPort -> BitMap);
  588.  
  589.             }
  590.  
  591.  
  592.             if (BitMap)
  593.             {
  594.  
  595.                 ULONG   *ColourTable;
  596.                 ULONG    ModeID;
  597.                 LONG     NumColours;
  598.  
  599.                 NumColours = Scr -> ViewPort . ColorMap -> Count;
  600.  
  601.                 ModeID = BestModeID (BIDTAG_NominalWidth,     Pic -> Width,
  602.                                      BIDTAG_NominalHeight,    Pic -> Height,
  603.                                      BIDTAG_Depth,            Pic -> Depth,
  604.                                      TAG_END);
  605.  
  606.                 RPort -> BitMap = BitMap;
  607.  
  608.  
  609.                 if (fullscreen)
  610.                     ClipBlit (&Scr -> RastPort, 0, 0, RPort, 0, 0, Pic -> Width, Pic -> Height, 0xC0);
  611.                 else
  612.                     ClipBlit (Win -> RPort, 0, 0, RPort, 0, 0, Pic -> Width, Pic -> Height, 0xC0);
  613.  
  614.                 WaitBlit ();
  615.  
  616.                 if (ColourTable = (ULONG *) AllocVec (sizeof (ULONG) * 3 * NumColours, MEMF_ANY))
  617.                 {
  618.  
  619.                     Object   *Picture;
  620.  
  621.                     GetRGB32 (Scr -> ViewPort . ColorMap, 0, NumColours, ColourTable);
  622.  
  623.                     UnlockIBase (IntuiLock);
  624.  
  625.                     Locked = FALSE;
  626.  
  627.                     if (Picture = NewDTObject (PRG_TITLE"BitMap", DTA_SourceType,    DTST_RAM,
  628.                                                                   DTA_GroupID,       GID_PICTURE,
  629.                                                                   PDTA_NumColors,    NumColours,
  630.                                                                   PDTA_BitMap,       BitMap,
  631.                                                                   PDTA_ModeID,       ModeID,
  632.                                                                   TAG_END))
  633.                                                                   {
  634.  
  635.                                                                       struct    ColorRegister   *ColourMap;
  636.                                                                       struct    BitMapHeader    *BitMapHeader;
  637.                                                                       ULONG                     *Colours;
  638.  
  639.  
  640.                                                                       if (GetDTAttrs (Picture, PDTA_BitMapHeader,     &BitMapHeader,
  641.                                                                                                PDTA_ColorRegisters,   &ColourMap,
  642.                                                                                                PDTA_CRegs,            &Colours,
  643.                                                                                                TAG_END) == 3)
  644.                                                                                                {
  645.  
  646.                                                                                                    BitMapHeader -> bmh_Left       = Pic -> Left;
  647.                                                                                                    BitMapHeader -> bmh_Top        = Pic -> Top;
  648.                                                                                                    BitMapHeader -> bmh_Width      = Pic -> Width;
  649.                                                                                                    BitMapHeader -> bmh_Height     = Pic -> Height;
  650.                                                                                                    BitMapHeader -> bmh_Depth      = Pic -> Depth;
  651.                                                                                                    BitMapHeader -> bmh_PageWidth  = Pic -> PageWidth;
  652.                                                                                                    BitMapHeader -> bmh_PageHeight = Pic -> PageHeight;
  653.  
  654.                                                                                                    CopyMem (ColourTable, Colours, 3 * sizeof (ULONG) * NumColours);
  655.  
  656.                                                                                                    while (NumColours--)
  657.                                                                                                    {
  658.  
  659.                                                                                                        ColourMap -> red   = (UBYTE) ((*Colours++) >> 24);
  660.                                                                                                        ColourMap -> green = (UBYTE) ((*Colours++) >> 24);
  661.                                                                                                        ColourMap -> blue  = (UBYTE) ((*Colours++) >> 24);
  662.  
  663.                                                                                                        ColourMap++;
  664.  
  665.                                                                                                    }
  666.  
  667.                                                                                                    FreeVec (ColourTable);
  668.                                                                                                    FreeVec (RPort);
  669.  
  670.                                                                                                    return (Picture);
  671.  
  672.                                                                                                }
  673.                                                                                                else
  674.                                                                                                {
  675.  
  676.                                                                                                    printf ("ERROR: GetDTAttrs() failed.\n");
  677.  
  678.                                                                                                }
  679.  
  680.                                                                       DisposeDTObject (Picture);
  681.  
  682.                                                                       BitMap = NULL;
  683.  
  684.                                                                   }
  685.                                                                   else
  686.                                                                   {
  687.  
  688.                                                                       printf ("ERROR: NewDTObject failed.\n");
  689.  
  690.                                                                   }
  691.  
  692.                     FreeVec (ColourTable);
  693.  
  694.                 }
  695.  
  696.                 FreeBitMap (BitMap);
  697.  
  698.             }
  699.  
  700.             if (Locked)
  701.                 UnlockIBase (IntuiLock);
  702.  
  703.             FreeVec (Pic);
  704.  
  705.         }
  706.  
  707.         FreeVec (RPort);
  708.  
  709.     }
  710.  
  711.     return (NULL);
  712.  
  713. }
  714.  
  715. ///
  716.